package com.example.lenovo.test1;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class Main9Activity extends AppCompatActivity {
    private int[] draws = {
            R.mipmap.a2,R.mipmap.a7,R.mipmap.a5,R.mipmap.a14};
    private ViewPager vp;
    private LinearLayout ll;
    private TextView[] points=new TextView[draws.length];
    private ImageView[] images=new ImageView[draws.length];
    private class MyClick implements View.OnClickListener{
        private int index;

        public MyClick(int index) {
            this.index = index;
        }

        @Override
        public void onClick(View v) {
            vp.setCurrentItem(index);
        }
    }
    private boolean isstop=false;
    private int index=0;
    private int count=0;
    public void thread(){
        new Thread(){
            @Override
            public void run() {
                super.run();

                while (true){
                    while (!isstop){
                        try {
                            count++;
                            if (count%20==0){
                                handler.sendEmptyMessage(123);
                            }
                            sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==123){
                vp.setCurrentItem((index++)%draws.length);
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main9);
        vp=findViewById(R.id.vp);
        ll=findViewById(R.id.ll);
        for (int i = 0; i <images.length ; i++) {
            images[i]=new ImageView(this);
            ViewPager.LayoutParams params=
                    new ViewPager.LayoutParams();
            images[i].setLayoutParams(params);
            images[i].setBackgroundResource(draws[i]);
            points[i]=new TextView(this);
            LinearLayout.LayoutParams params1=
                    new LinearLayout.LayoutParams(20,20);
            params1.setMargins(10,0,10,0);
            points[i].setLayoutParams(params1);
            points[i].setText((i+1)+"");
            points[i].setGravity(Gravity.CENTER);
            points[i].setTextSize(5);
            points[i].setTextColor(Color.BLACK);
            points[i].setOnClickListener(new MyClick(i));
            points[i].setBackgroundResource(R.drawable.tv_point_white);
            ll.addView(points[i]);
        }
        points[0].setTextColor(Color.WHITE);
        points[0].setBackgroundResource(R.drawable.tv_point_red);
        vp.setAdapter(adapter);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index=position;
                for (int i = 0; i <points.length ; i++) {
                    points[i].setBackgroundResource
                            (R.drawable.tv_point_white);
                    points[i].setTextColor(Color.BLACK);
                }
                points[position].setTextColor(Color.WHITE);
                points[position].setBackgroundResource
                        (R.drawable.tv_point_red);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        thread();
        vp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        isstop=true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        isstop=false;
                        break;
                }
                return false;
            }
        });
    }

    private PagerAdapter adapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(images[position]);
            return images[position];
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(images[position]);
        }
    };
}